package cn.jdy.framework.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

/**
 * <pre>
 * 类描述 提供js,css文件混淆压缩,合并
 * 功能描述  
 */
public class JsCssUtil {
    
    /**
     * 合并压缩多个js文件到一个文件
     * @param String[]　待压缩的js文件路径列表
     * @param outfile   压缩后的文件保存列表
     */
    public static void compress(String[] files,String path,File outfile){
        List<File> fileList = new ArrayList<File>();
        for (int i = 0; i < files.length; i++) {
            fileList.add(new File(path+files[i]));
        }
        compress(fileList,outfile);
    }
    
     /**
      * 合并压缩多个js文件到一个文件
      * @param fileList　待压缩的js文件列表
      * @param outfile   压缩后的文件保存列表
      */
     public static void compress(List<File> fileList,File outfile){
         Writer out = null;
         try {
             out = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8");
             for (File file : fileList) {
                 String fileType = StringUtil.getFileType(file);//js|css
                 Reader in = new InputStreamReader(new FileInputStream(file.getPath()), "UTF-8");
                 compress(fileType,in,out);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             try {
                 if(out != null)
                     out.close();
             } catch (IOException e) {
             }
         }
     }
      
     /**
      * 压缩单个文件到outfile文件中
      * @param srcfile　待压缩的js文件
      * @param outfile   压缩后的文件
      */
     public static void compress(File srcfile,File outfile){
         Writer out = null;
         try {
             out = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8");
             String fileType = StringUtil.getFileType(srcfile);//js|css
             Reader in = new InputStreamReader(new FileInputStream(srcfile), "UTF-8");
             compress(fileType,in,out);
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             try {
                 if(out != null)  out.close();
             } catch (IOException e) {
             }
         }
     }
     
     /**
      * 压缩单个文件到outfile文件中
      * @param srcfile　待压缩的js文件
      * @param out   压缩后的文件数据流
      */
     public static void compress(File srcfile,Writer out){
         try {
             String fileType = StringUtil.getFileType(srcfile);//js|css
             Reader in = new InputStreamReader(new FileInputStream(srcfile), "UTF-8");
             compress(fileType,in,out);
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             try {
                 if(out != null)  out.close();
             } catch (IOException e) {
             }
         }
     }

     /**
      * 修改com.yahoo.platform.yui.compressor.YUICompressor以方便java内部调用
      * @param args 参见YUICompressor调用参数
      * @param out 注意 数据输出流，需要在外部关闭流
      */
     private static void compress(String type,Reader in,Writer out){
         try {
             int linebreakpos = -1;
             if (type.equalsIgnoreCase("js")) {
                 try {
                     JavaScriptCompressor compressor = new JavaScriptCompressor(in,null);
                     in.close(); in = null;
                                        //out,-1,true,false,true,false
                     compressor.compress(out, linebreakpos, true, false, true, false);
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             } else if (type.equalsIgnoreCase("css")) {
                 CssCompressor compressor = new CssCompressor(in);
                 in.close(); in = null;
                 compressor.compress(out, linebreakpos);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } finally {
             if (in != null) {
                 try {
                     in.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }
     }
}
